package com.credithc.signature.common.utils;

import cfca.seal.bean.CertApplyInfoBean;
import cfca.seal.bean.NamedSealInfoBean;
import cfca.seal.bean.SealInfoBean;
import cfca.seal.bean.SealUserInfo;
import cfca.seal.bean.SignLocation;
import cfca.seal.util.StrategyUtil;
import com.credithc.signature.common.constant.CoreConstant;
import com.credithc.signature.common.to.CFCASignParam;
import com.credithc.signature.common.to.CFCASignTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年02⽉07⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
public class GenerateUtil {

    /**
     * 动态设置私章印模图片大小
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generateImageWidth(CFCASignTO cfcaSignTO) {
        String imageFinalWidth = CoreConstant.IMAGE_WIDTH;
        int len = CNUtil.getWordsLen(cfcaSignTO.getUserName());
        if (len > 3) {
            int imageWidth = Integer.valueOf(imageFinalWidth);
            int temp = (len - 3) * 10;
            int total = imageWidth + temp;
            if (total >= 200) {
                total = 200;
            }
            imageFinalWidth = String.valueOf(total);
        }
        return imageFinalWidth;
    }

    /**
     * 生成私章编码
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generateSealCode(CFCASignTO cfcaSignTO) {
        String idTypeCode = cfcaSignTO.getIdTypeCode();
        String userIdNo = cfcaSignTO.getUserIdNo();
        return "PER_" + userIdNo + "_" + idTypeCode;
    }

    /**
     * 生成【私章签章】请求参数
     *
     * @param param
     * @return
     */
    public static String generatePersonalStrategyXML(CFCASignParam param, String cfca_personal_location, int cfca_personal_offset_x, int cfca_personal_offset_y, String cfca_personal_code_style) {
        ArrayList<String> sealStrategyList = (ArrayList<String>) param.getCfcaSealInfoTOList().stream().map(cfcaSignTO -> {
            String userName = cfcaSignTO.getUserName();
            SealUserInfo sealUserInfo = new SealUserInfo(userName, cfcaSignTO.getIdTypeCode(), cfcaSignTO.getUserIdNo(), CoreConstant.SEAL_FLAG);
            String Keyword = "$_" + userName + "_$";
            String locationStyle = cfca_personal_location;
            String personalSealPos = cfcaSignTO.getPersonalSealPos();
            if (StringUtils.isNotBlank(personalSealPos)) {
                locationStyle = personalSealPos;
            }
            SignLocation signLocation = new SignLocation(Keyword, locationStyle, cfca_personal_offset_x, cfca_personal_offset_y);
            String sealCode = cfcaSignTO.getSealCode();
            String sealPassword = cfcaSignTO.getSealPassword();
            String sealStrategyXML = null;
            try {
                sealStrategyXML = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, cfcaSignTO.getSealImage(), CoreConstant.BUSINESS_CODE, cfca_personal_code_style, sealUserInfo, signLocation);
            } catch (Exception e) {
                log.error("制作私章策略文件发生异常......", e);
            }
            return sealStrategyXML;
        }).collect(Collectors.toList());
        return StrategyUtil.createBatchSealStrategyXML(sealStrategyList);
    }

    /**
     * 生成【私章证书】请求参数
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generatePersonalCertApplyXml(CFCASignTO cfcaSignTO) {
        CertApplyInfoBean certApplyInfoBean = new CertApplyInfoBean(CoreConstant.CFCA_NAME_SEAL_CUSTOMER_TYPE, CoreConstant.CFCA_KEYALG,
                CoreConstant.KEYLENGTH, cfcaSignTO.getUserName(), cfcaSignTO.getIdTypeCode(), cfcaSignTO.getUserIdNo());
        return StrategyUtil.createCertApplyXml(certApplyInfoBean);
    }

    /**
     * 生成【私章信息】请求参数
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generatePersonalSealStrategyXML(CFCASignTO cfcaSignTO) {
        String imageFinalWidth = generateImageWidth(cfcaSignTO);
        if (StringUtils.isBlank(cfcaSignTO.getSealCode())) {
            String sealCode = GenerateUtil.generateSealCode(cfcaSignTO);
            cfcaSignTO.setSealCode(sealCode);
        }
        if (StringUtils.isBlank(cfcaSignTO.getSealPassword())) {
            cfcaSignTO.setSealPassword(CoreConstant.DEFALUT_PASS);
        }
        NamedSealInfoBean namedSealInfoBean = new NamedSealInfoBean(CoreConstant.SEAL_PERSON, CoreConstant.SEAL_ORG, cfcaSignTO.getUserName(),
                cfcaSignTO.getSealCode(), cfcaSignTO.getSealPassword(), CoreConstant.IMAGE_SHAPE, imageFinalWidth, CoreConstant.IMAGE_HEIGHT, CoreConstant.COLOR, CoreConstant.FONTSIZE,
                CoreConstant.ORG_CODE, CoreConstant.SEAL_FLAG);
        return StrategyUtil.createNamedSealInfoXml(namedSealInfoBean);
    }

    /**
     * 生成【公章证书】请求参数
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generateOfficialCertApplyXml(CFCASignTO cfcaSignTO) {
        String newSealCode = "OFF" + UUIDUtil.getRandomFixedLengthCode(27);
        cfcaSignTO.setSealCode(newSealCode);
        CertApplyInfoBean certApplyInfoBean = new CertApplyInfoBean(CoreConstant.CFCA_OFFICIAL_SEAL_CUSTOMER_TYPE, CoreConstant.CFCA_KEYALG,
                CoreConstant.KEYLENGTH, cfcaSignTO.getUserName(), cfcaSignTO.getIdTypeCode(), cfcaSignTO.getUserIdNo());
        return StrategyUtil.createCertApplyXml(certApplyInfoBean);
    }

    /**
     * 生成【公章信息】请求参数
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generateOfficialSealInfoXml(CFCASignTO cfcaSignTO) {
        SealInfoBean sealInfoBean = new SealInfoBean(cfcaSignTO.getSealPerson(), cfcaSignTO.getSealName(), cfcaSignTO.getSealName(),
                cfcaSignTO.getSealCode(), cfcaSignTO.getSealPassword(), CoreConstant.OFFICIAL_SEAL_TYPE, cfcaSignTO.getImageCode(),
                CoreConstant.ORG_CODE, CoreConstant.SEAL_FLAG);
        sealInfoBean.setSealImageSize(cfcaSignTO.getSealImageSize());
        if (StringUtils.isBlank(sealInfoBean.getSealImageSize())) {
            sealInfoBean.setSealImageSize(CoreConstant.SEAL_IMAGE_SIZE);
        }
        return StrategyUtil.creatSealInfoXML(sealInfoBean);
    }

    /**
     * 生成【公章签章】请求参数
     *
     * @param cfcaSignTO
     * @return
     */
    public static String generateOfficialStrategyXML(CFCASignTO cfcaSignTO, int cfca_offical_offset_x, int cfca_offical_offset_y, String cfca_offical_code_style) throws Exception {
        SealUserInfo sealUserInfo = new SealUserInfo(cfcaSignTO.getSealPerson(), cfcaSignTO.getKeyWord(), CoreConstant.CFCA_SEAL_REASON);
        SignLocation signLocation = new SignLocation(cfcaSignTO.getKeyWord(), CoreConstant.LOCATION_STYLE,
                cfca_offical_offset_x, cfca_offical_offset_y);
        return StrategyUtil.createSealStrategyXml(cfcaSignTO.getSealCode(), cfcaSignTO.getSealPassword(),
                CoreConstant.BUSINESS_CODE, cfca_offical_code_style, sealUserInfo, signLocation);
    }

}
